package com.cangxuan.manage.controller;

import com.cangxuan.manage.annotation.Check;
import com.cangxuan.manage.annotation.check.Max;
import com.cangxuan.manage.annotation.check.NotNull;
import com.cangxuan.manage.bean.Page;
import com.cangxuan.manage.entity.Article;
import com.cangxuan.manage.entity.Goods;
import com.cangxuan.manage.service.IArticleService;
import io.swagger.annotations.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章相关控制器
 * Author:罗健
 */
@Api(tags = "文章相关接口")
@RestController
@RequestMapping("article")

public class ArticleController extends BaseController {
    IArticleService articleService;
    @Autowired
    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }
    @Data
    @ApiModel("文章分页查询body")
    public static class articlePageBody{
        @ApiModelProperty("页码，从1开始")
        @NotNull(message = "请传递pageIndex")
        Integer pageIndex;
        @ApiModelProperty("页大小，就是每页查询的数量，最大为50条")
        @Max(value = 50, message = "pageSize不能大于50")
        Integer pageSize = 10;
        @ApiModelProperty("文章类型ID")
        Integer typeId;
        @ApiModelProperty("文章标题")
        String title;
    }
    /**
     * 分页获取文章
     * @param articlePageBody
     * @return
     */
    @PostMapping
    @ApiOperation("文章分页查询，按文章类型分页，1公告，2新闻")
    public Object page(@Check @RequestBody ArticleController.articlePageBody articlePageBody) {
        Page<Article> articlePage = articleService.page(articlePageBody.getPageIndex(),
                articlePageBody.getPageSize(),
                articlePageBody.getTypeId(),
                articlePageBody.getTitle());
        return success(articlePage);
    }
    /**
     * 增加
     * @param article
     * @return
     */
    @PostMapping("save")
    @ApiOperation("保存文章，增加和修改")
    public Object save(@Check @RequestBody Article article) {
        boolean save = articleService.save(article);
        return success(save);
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @GetMapping("remove")
    @ApiOperation("删除文章")
    public Object remove(@RequestParam int id) {
        articleService.remove(id);
        return success();
    }

}
